﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using YCAMDPLAN;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System.Text;
namespace WPFExcelReport
{
    public partial class YCAMDPLANData : BaseFunction
    {
        string Today
        {
            get
            {
                return System.DateTime.Now.Year.ToString()
                    + Right("00" + System.DateTime.Now.Month.ToString(), 2)
                    + Right("00" + System.DateTime.Now.Day.ToString(), 2);
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        public YCAMDPLANData()
            : base()
        {
        }
        public YCAMDPLANData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public YCAMDPLANData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet LoadGXDECKData()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = null;
            try
            {
                //刷新数据，若有新的需求则插入
                INTEXDBC.InsertICC_ABAUFM(null, 1);

                ///读取汇总信息                
                dwData = INTEXDB.MDECK.NewRow();
                dwData["MDMANDANT"] = "00";
                dwData["MDTYP"] = "B";
                SResult = INTEXDBC.QueryMDECK(dwData, 9);
                if (SResult.Tables.Count > 0)
                {
                    SResult.Tables[0].TableName = "ICC_XDECK";
                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadXDECKData(DataRow dwInput, bool IsPlanned)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = null;
            try
            {
                //MBST EKPOS MDECK ABPOS ICC_ABAUFM
                string client = dwInput["MANDANT"].ToString();
                string wakz = " ";
                string art = dwInput["ART"].ToString();
                string var = dwInput["VAR"].ToString();
                string color = "          ";
                string size = "       ";
                //读取库存
                dwData = INTEXDB.MBST.NewRow();
                dwData["MBMANDANT"] = client;
                dwData["MBMAT"] = Left(art + "                                    ", 14);
                dwData["MBVAR"] = var;
                SResult = INTEXDBC.QueryICC_MBST(dwData, 2);
                SResult.Tables[0].TableName = "ICC_STOCK";
                SReturn.Merge(SResult);
                //读取采购              
                art = Left(art + "                                              ", 20);
                SResult = INTEXDBC.QueryICC_EKPOS(client, wakz, art, var, color,size, "",2);
                SReturn.Merge(SResult);
                //读取锁定数量
                CheckLockedQty(ref SReturn, dwInput);
                //读取明细
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] =client;
                dwData["ABAART"] = Left(art + "                                    ", 20);
                dwData["ABAVAR"] = var;
                dwData["ABAFARBE"] = color;
                dwData["ABAGROE"] = "       ";
                if (IsPlanned)
                {
                    SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 9);
                }
                else
                {
                    SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 10);
                }
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryABPOS(dwData, 8);
                SReturn.Merge(SResult);

                //SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 8);
                //SReturn.Merge(SResult); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void CheckLockedQty(ref DataSet src, DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataRow dwData = null;


            dwData = INTEXDB.ICC_ABAUFM.NewRow();
            dwData["ABAMANDANT"] = dwInput["MANDANT"];
            dwData["ABAART"] = Left(dwInput["ART"].ToString() + "                                    ", 20);
            dwData["ABAVAR"] = dwInput["VAR"];
            dwData["ABAFARBE"] = dwInput["COLOR"];
            dwData["ABAGROE"] = dwInput["ASIZE"];
            SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 5);
            foreach (DataRow rz in SResult.Tables[0].Rows)
            {
                string sptype = rz["ABAAUFTYP"].ToString();
                switch (sptype)
                {
                    case "N":
                        break;
                    case "B":
                        var query1 = from DataRow rwz in src.Tables["ICC_STOCK"].AsEnumerable()
                                     where rwz["LO"].ToString().Trim() == rz["ABAAUFKRS"].ToString().Trim()
                                     && rwz["LB"].ToString().Trim() == rz["ABAAUFLFD"].ToString().Trim()
                                     select rwz;
                        foreach (DataRow rwz in query1)
                        {
                            rwz["QTYLOCKED"] = rz["ABAQTYALLOCATE"];
                            rwz["QTYFREE"] = System.Convert.ToDouble(rwz["QTY"].ToString()) - System.Convert.ToDouble(rwz["QTYLOCKED"].ToString());
                        }
                        break;
                    case "P":
                        var query2 = from DataRow rwz in src.Tables["ICC_EKPOS"].AsEnumerable()
                                     where rwz["EKPAUFKRS"].ToString().Trim() == rz["ABAAUFKRS"].ToString().Trim()
                                     && rwz["EKPAUFLFD"].ToString().Trim() == rz["ABAAUFLFD"].ToString().Trim()
                                     && rwz["EKPAUFPOS"].ToString().Trim() == rz["ABAAUFPOS"].ToString().Trim()
                                     select rwz;
                        foreach (DataRow rwz in query2)
                        {
                            rwz["EKPQTYLOCKED"] = rz["ABAQTYRESERVE"];
                            rwz["EKPQTYFREE"] = System.Convert.ToDouble(rwz["EKPME1"].ToString()) - System.Convert.ToDouble(rwz["EKPQTYLOCKED"].ToString());
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public DataSet LoadSupplier()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            SResult = INTEXDBC.QueryLIEFDAT(dwData, 3);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet CreateTempEKPOS(DataTable tbEKPOS)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            List<string> listsup = new List<string>();
            ASCIIEncoding ascII = new System.Text.ASCIIEncoding();
            try
            {
                //读取MS，获取默认供应商，如果没有则是0
                string art = "", var = "";
                string supplier = "00000000";
                string procesed = "00000000;";
                listsup.Add(supplier);
                /*界面：  	EKPNEUB EKPNEUDB EKPLTERM  EKPLTERM EKPMEEHFKT EKPME1 EKPSMENGE  EKPPRME EKPPREH  	
                 * EKPARTGR EKPART 	EKPVAR 	EKPFARBE           	EKPDESS          	EKPCOL
                 * EKPBESCH    	EKPZUSTAND       	EKPGROE
                 此处        	EKPAUFGRP BE	EKPAUFART  
8	
000001000000	EKPMEEHFKT
*/
                foreach (DataRow rw in tbEKPOS.Rows)
                {
                    rw["EKPARTGR"] = "8";
                    rw["EKPLIEFNR"] = "00000000";
                    rw["EKPAUFGRP"] = "B";
                    rw["EKPAUFART"] = "BE";
                    art = rw["EKPART"].ToString();// ascII.GetString((byte[])rw["EKPART"]);
                    var = rw["EKPVAR"].ToString();// ascII.GetString((byte[])rw["EKPVAR"]);
                    DataRow rms = INTEXDB.MSEINK.NewMSEINKRow();
                    rms["MEMANDANT"] = "00";
                    rms["MEMAT"] = Left(art, 14);
                    rms["MEVAR"] = Left(var, 3);
                    STemp = INTEXDBC.QueryMSEINK(rms, 2);
                    foreach (DataRow rtemp in STemp.Tables[0].Rows)
                    {
                        supplier = rtemp["MELIEFNR"].ToString();
                        rw["EKPLIEFNR"] = supplier;
                        rw["EKPPRME"] = Right("0000000000" + (System.Convert.ToDouble(rtemp["MEPREIS1"].ToString())).ToString(), 10);
                        rw["EKPPREH"] = rtemp["MEPREISME"].ToString();
                        rw["EKPMEEHFKT"] = rtemp["MEPREHFKT"].ToString();
                        int leadtime=System.Convert.ToInt16(rtemp["MELIEFZT"].ToString());
                        rw["EKPLTERM"] = GetDateAddDays(Today, leadtime);
                        if (!procesed.Contains(supplier + ";"))
                        {
                            procesed += supplier + ";";
                            listsup.Add(supplier);
                        }
                    }
                }
                foreach (string sup in listsup)
                {
                    int pos = 1;
                    string lfd = "";
                    string krs = "";
                    var query = from DataRow ekp in tbEKPOS.AsEnumerable()
                                where ekp["EKPLIEFNR"].ToString() == sup
                                select ekp;
                    foreach (DataRow ekp in query)
                    {
                        krs = ekp["EKPAUFKRS"].ToString();
                        if (lfd == "")
                        {
                            lfd = GetAUFLFD(krs, "EKAUFTR");
                        }
                        ekp["EKPAUFLFD"] = lfd;
                        ekp["EKPAUFPOS"] = Right("000" + pos, 3);
                        pos++;
                    }
                }
                //赋予供应商
                //根据供应商情况，组合采购单，产生EKKOPF
                SResult.Merge(tbEKPOS);
                SResult.Tables[0].TableName = "ICC_EKPOS";
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
 
        public DataSet LoadABPOSData(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABAVKAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABAVKAUFLFD"] = dwInput["ABPAUFLFD"];
                dwData["ABAVKAUFPOS"] = dwInput["ABPAUFPOS"];
                SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 7);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void Send2Intex(Dictionary<string, object> dicParamater)
        {
            try
            {
                YCAAUTOEKData doc = new YCAAUTOEKData(OracleDB, proxy);
                doc.AutoRun();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void CheckData(DataSet src, ref bool IsSuccess, ref string msg)
        {
            try
            {
                DataTable tb = src.Tables["ICC_EKPOS"];
                foreach (DataRow rw in tb.Rows)
                {
                    string value = rw["EKPPRME"].ToString().Trim();
                    string order = rw["EKPAUFKRS"].ToString() + rw["EKPAUFLFD"].ToString() + rw["EKPAUFPOS"].ToString();
                    if (value == "" || value == "0")
                    {
                        msg += "请输入" + order + "价格!";
                        IsSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}